package net.javalib.gvmax.post.server;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class DAO {
	private DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
	
	public String getPostUrl(String email) {
		try {
			Entity entity = ds.get(key(email));
			return (String) entity.getProperty("url");
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public void setPostUrl(String email, String url) {
		Entity entity = new Entity(key(email));
		entity.setProperty("url", url);
		ds.put(entity);
	}
	
	
	private Key key(String email) {
		return KeyFactory.createKey("urls", email);
	}
	
}
